package pe.gob.osinergmin.activity.dialog;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

import pe.gob.osinergmin.R;
import pe.gob.osinergmin.activity.MenuActivity;
import pe.gob.osinergmin.activity.generic.GenericDialog;
import pe.gob.osinergmin.service.UnidadOperativaService;
import pe.gob.osinergmin.service.impl.UnidadOperativaServiceImpl;
import pe.gob.osinergmin.utils.PuntoControlConstants;

public class PlacaDialog extends GenericDialog implements View.OnClickListener {

    private Intent intent;

    private UnidadOperativaService unidadOperativaService;

    private MenuActivity menuActivity;

    private String[] placaList;

    public PlacaDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_placa);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        getWindow().setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);

        TextView btnPlacaContinuar = (TextView) findViewById(R.id.btnPlacaContinuar);
        TextView btnPlacaCancelar = (TextView) findViewById(R.id.btnPlacaCancelar);

        btnPlacaContinuar.setOnClickListener(this);
        btnPlacaCancelar.setOnClickListener(this);

        unidadOperativaService = new UnidadOperativaServiceImpl();

        try {
            placaList = getUnidadOperativaService().getUnidadOperativaPlacaList(getContext());
        }catch(Exception e){
            e.printStackTrace();
        }
        // Se crea el adaptador de autocomplete
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, placaList);

        AutoCompleteTextView txtPlaca = (AutoCompleteTextView) findViewById(R.id.txtPlaca);
        txtPlaca.setThreshold(2); // in 1 characteres will show autocomplete
        txtPlaca.setAdapter(arrayAdapter);
        txtPlaca.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    int index = -1;
                    for(int i = 0; i < placaList.length; i++){
                        String placa = placaList[i];
                        TextView _view = (TextView)view;
                        if(placa.equals(_view.getText().toString())){
                            index = i;
                            break;
                        }
                    }
                    if(index != -1) {
                        PuntoControlConstants.unidadOperativa = getUnidadOperativaService().getUnidadOperativaList(getContext(), null).get(index);
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnPlacaContinuar:
                // Ocultamos el Dialog para mostrar el progress
                if(PuntoControlConstants.unidadOperativa != null){
                    menuActivity.startActivity(intent);
                    this.dismiss();
                }else{
                    makeText(menuActivity, R.string.dialog_placa_validate, Toast.LENGTH_LONG);
                }
                break;
            case R.id.btnPlacaCancelar:
                dismiss();
                break;
            default:
                break;
        }
    }

    public void setIntent(Intent intent){
        this.intent = intent;
    }

    public UnidadOperativaService getUnidadOperativaService(){
        return this.unidadOperativaService;
    }

    public void setMenuActivity(MenuActivity menuActivity){
        this.menuActivity = menuActivity;
    }
}